package com.yupi.springbootinit.controller;

import com.yupi.springbootinit.common.BaseResponse;
import com.yupi.springbootinit.common.ErrorCode;
import com.yupi.springbootinit.common.ResultUtils;
import com.yupi.springbootinit.exception.BusinessException;
import com.yupi.springbootinit.exception.ThrowUtils;
import com.yupi.springbootinit.model.dto.course.CourseWatchRecordRequest;
import com.yupi.springbootinit.model.entity.User;
import com.yupi.springbootinit.model.vo.CourseVO;
import com.yupi.springbootinit.model.vo.CourseWatchStatsVO;
import com.yupi.springbootinit.service.CourseWatchService;
import com.yupi.springbootinit.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/course/watch")
@Slf4j
public class CourseWatchController {

    @Resource
    private CourseWatchService courseWatchService;

    @Resource
    private UserService userService;

    /**
     * 记录观看时长
     */
    @PostMapping("/record")
    public BaseResponse<Boolean> recordWatchDuration(@RequestBody CourseWatchRecordRequest request,
            HttpServletRequest httpServletRequest) {
        HttpSession session = httpServletRequest.getSession(false);
        if (session != null) {
            log.info("RecordWatch - Session ID: {}", session.getId());
            log.info("RecordWatch - Session is new: {}", session.isNew());
        } else {
            log.warn("RecordWatch - No session found");
        }
        
        if (request == null || request.getCourseId() <= 0 
            || request.getChapterId() <= 0 
            || request.getDuration() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(httpServletRequest);
        ThrowUtils.throwIf(loginUser == null, ErrorCode.NOT_LOGIN_ERROR);
        
        boolean result = courseWatchService.recordWatchDuration(
            request.getCourseId(), 
            request.getChapterId(), 
            request.getDuration(), 
            loginUser
        );
        return ResultUtils.success(result);
    }

    /**
     * 获取用户观看统计
     */
    @GetMapping("/stats")
    public BaseResponse<List<CourseWatchStatsVO>> getWatchStats(HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        ThrowUtils.throwIf(loginUser == null, ErrorCode.NOT_LOGIN_ERROR);
        
        List<CourseWatchStatsVO> statsList = courseWatchService.getUserWatchStats(loginUser.getId());
        return ResultUtils.success(statsList);
    }

    /**
     * 获取推荐课程
     */
    @GetMapping("/recommend")
    public BaseResponse<List<CourseVO>> getRecommendCourses(HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        ThrowUtils.throwIf(loginUser == null, ErrorCode.NOT_LOGIN_ERROR);
        
        List<CourseVO> recommendCourses = courseWatchService.getRecommendCourses(loginUser.getId());
        return ResultUtils.success(recommendCourses);
    }
} 